<?php

$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;

if ($id > 0) {    
    $entryrow = Db::getOneRow("*", 'article', 'id=' . $id);    
} else {
    Js::gourl(baseUrl() . '/info');
    exit;
}
if (Http::isPost()) {
    $title = Form::input('title', 'ne');
    $intro = Form::input('intro');
    $content = Form::input('content');
    $title2 = Form::input('title2', 'ne');
    $intro2 = Form::input('intro2');
    $content2 = Form::input('content2');
    $posteddate = date('H:i d-m-Y');
    $img = '';
    if (Form::isValid()) {
        if ($id > 0) {
            $curpath = $_SERVER['DOCUMENT_ROOT'] . str_replace('/admin', '/upload/', baseUrl());
            $imagename = Db::getOneField('img', 'article', 'id=' . $id);
            if ($_FILES["file"]["error"] > 0 && $_FILES["file"]["error"] != '4') {
                echo "Có lổi: " . $_FILES["file"]["error"] . "<br />";
            } else if ($_FILES["file"]["error"] == '4') {
                $eval = eval(Db::update('article', "id=$id", array_merge(array('id', 'img','type','nation','level'))));
                if (mysql_affected_rows() > 0) {
                    Js::alert(fl('success_update'));
                    Js::GoUrl(baseUrl() . '/info');
                    exit();
                }
            } else {
                $upload = Http::UploadImage('file', str_replace('//', '/', $curpath), '', $img);
                if ($upload === '') {
                    $eval = eval(Db::update('article', "id=$id", array_merge(array('id','type','nation','level'))));
                    if (mysql_affected_rows() > 0) {
                        unlink($curpath . $imagename);
                        Js::alert(fl('success_update'));
                        Js::GoUrl(baseUrl() . '/info');
                        exit();
                    }
                } else {
                    Js::alert($upload);
                }
            }
        }
    }
}
?>
